package com.itzxx.huazhun.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.navigation.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.itzxx.huazhun.R
import com.itzxx.huazhun.data.model.EventMessage
import com.itzxx.huazhun.data.model.EventType
import com.itzxx.huazhun.databinding.ActivityMainBinding
import com.itzxx.huazhun.ui.collection.CollectionFragment
import com.itzxx.huazhun.ui.device.DeviceFragment
import com.itzxx.huazhun.ui.record.RecordFragment
import com.itzxx.huazhun.ui.testing.TestingFragment
import com.itzxx.huazhun.ui.user.UserManagementFragment
import com.itzxx.huazhun.utils.UserCacheHelper
import com.itzxx.huazhun.utils.ble.BleHelper
import com.itzxx.library_base_ktx.base.activity.BaseVmDbActivity
import com.itzxx.library_base_ktx.ext.util.logd
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MainActivity : BaseVmDbActivity<MainViewModel, ActivityMainBinding>() {

    companion object {
        fun startActivity(context: Context, isOffline: Boolean = false) {
            val intent = Intent(
                context,
                MainActivity()::class.java
            )
            intent.putExtra("isOffline", isOffline)
            context.startActivity(
                intent
            )
        }
    }

    private lateinit var navView:BottomNavigationView

    override fun databind2ViewModel() {
        mDatabind.viewmodel = mViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)

        val viewPagerFragmentStateAdapter =
            ViewPagerFragmentStateAdapter(supportFragmentManager, lifecycle)
        val isOffline = intent.getBooleanExtra("isOffline", false)
        UserCacheHelper.putIsOffline(isOffline)
        mViewModel.isOfflineLiveData.postValue(isOffline)
        if (isOffline){
            viewPagerFragmentStateAdapter.addFragment(TestingFragment())
            viewPagerFragmentStateAdapter.addFragment(CollectionFragment())
            //自定义底部Navigation
            mDatabind.navOfflineView.visibility = View.VISIBLE
            navView = mDatabind.navOfflineView
            navView.setOnItemSelectedListener { item ->
                when (item.title) {
                    "检测" -> mDatabind.viewpager2.setCurrentItem(0, false)
                    "采集管理" -> mDatabind.viewpager2.setCurrentItem(1, false)
                }
                true
            }
        }else{
            viewPagerFragmentStateAdapter.addFragment(DeviceFragment())
            viewPagerFragmentStateAdapter.addFragment(TestingFragment())
            viewPagerFragmentStateAdapter.addFragment(CollectionFragment())
            viewPagerFragmentStateAdapter.addFragment(RecordFragment())
            //自定义底部Navigation
            mDatabind.navView.visibility = View.VISIBLE
            navView = mDatabind.navView
            if (UserCacheHelper.isAdmin()) {
                navView.menu.add(
                    0,
                    navView.menu.size(),
                    navView.menu.size(),
                    getString(R.string.title_user)
                ).setIcon(R.mipmap.icon_user)
                viewPagerFragmentStateAdapter.addFragment(UserManagementFragment())
            }
            navView.setOnItemSelectedListener { item ->
                when (item.title) {
                    "设备管理" -> mDatabind.viewpager2.setCurrentItem(0, false)
                    "检测" -> mDatabind.viewpager2.setCurrentItem(1, false)
                    "采集管理" -> mDatabind.viewpager2.setCurrentItem(2, false)
                    "检测记录" -> mDatabind.viewpager2.setCurrentItem(3, false)
                    "用户管理" -> mDatabind.viewpager2.setCurrentItem(4, false)
                }
                true
            }
        }
        mDatabind.viewpager2.offscreenPageLimit = navView.menu.size()
        mDatabind.viewpager2.adapter = viewPagerFragmentStateAdapter
        mDatabind.viewpager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                navView.menu.getItem(position).setChecked(true)
            }
        })
        mDatabind.viewpager2.isUserInputEnabled = false
    }


    override fun createObserver() {

    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

    override fun onDestroy() {
        super.onDestroy()
        BleHelper.disconnect()
        EventBus.getDefault().unregister(this)
    }

    //接收事件
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    fun onReceiveMsg(message: EventMessage) {
        if (EventType.SWITCH_MAIN_PAGE == message.type) {
            mDatabind.viewpager2.setCurrentItem(message.arg1.toInt(), false)
        }
    }
}